package com.example;

import com.example.bean.Product;
import com.example.bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        System.out.println("=== Spring IOC容器基于XML配置示例 ===");

        // 1. 创建Spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("✅ Spring容器启动成功！");
        System.out.println("=====================================");

        // 2. 从容器中获取Product Bean
        Product product1 = (Product) context.getBean("product");
        Product product2 = (Product) context.getBean("product2");

        System.out.println("📦 获取到的Product Bean:");
        System.out.println("Product 1: " + product1);
        System.out.println("Product 2: " + product2);

        System.out.println("=====================================");

        // 3. 从容器中获取Student Bean
        Student student1 = (Student) context.getBean("student");
        Student student2 = (Student) context.getBean("student2");

        System.out.println("🎓 获取到的Student Bean:");
        System.out.println("Student 1: " + student1);
        System.out.println("Student 2: " + student2);

        System.out.println("=====================================");

        // 4. 验证Bean的单例特性
        Product product1Again = (Product) context.getBean("product");
        System.out.println("🔍 验证单例模式 - 两次获取的product1是否是同一个对象: " + (product1 == product1Again));

        System.out.println("=====================================");
        System.out.println("🎉 程序执行完成！");

        // 5. 关闭容器
        ((ClassPathXmlApplicationContext) context).close();
    }
}